package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junwell.common.dto.DataDto;
import com.junwell.common.lang.Result;
import com.junwell.entity.Finance;
import com.junwell.entity.Orders;
import com.junwell.mapper.FinanceMapper;
import com.junwell.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {

    @Autowired
    private FinanceMapper financeMapper;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private JedisPool jedisPool;



    @GetMapping("table")
    public List<DataDto> table(){

        QueryWrapper<Orders> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("type","0");//食品
        int a = ordersService.count(queryWrapper1);
        QueryWrapper<Orders> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("type","1");//电子产品
        int b = ordersService.count(queryWrapper2);
        QueryWrapper<Orders> queryWrapper3=new QueryWrapper<>();
        queryWrapper3.eq("type","2");//服装
        int c = ordersService.count(queryWrapper3);
        QueryWrapper<Orders> queryWrapper4=new QueryWrapper<>();
        queryWrapper4.eq("type","3");//日用品
        int d = ordersService.count(queryWrapper4);
        QueryWrapper<Orders> queryWrapper5=new QueryWrapper<>();
        queryWrapper5.eq("type","4");//家具
        int e = ordersService.count(queryWrapper5);
        List<DataDto> list =new ArrayList<>();
        list.add(new DataDto(a,"食品"));
        list.add(new DataDto(b,"电子产品"));
        list.add(new DataDto(c,"服装"));
        list.add(new DataDto(d,"日用品"));
        list.add(new DataDto(e,"家具"));
        Jedis jedis= jedisPool.getResource();
        jedis.zadd("top",a,"食品");
        jedis.zadd("top",b,"电子产品");
        jedis.zadd("top",c,"服装");
        jedis.zadd("top",d,"日用品");
        jedis.zadd("top",e,"家具");
        jedis.close();

        return list;


    }

    @GetMapping("top")
    public Result top(){
        Jedis jedis = jedisPool.getResource();
        Set<String> set = jedis.zrevrangeByScore("top", "+inf","-inf");
        jedis.close();
        return Result.succ(set);
    }

    @GetMapping("income")
    public  Result income(){
        QueryWrapper<Finance> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("income").orderByDesc("id").last("limit 7");
        List<Finance> finances = financeMapper.selectList(queryWrapper);
        List<Double> list=new ArrayList<>();
        for (Finance finance : finances) {
            list.add(finance.getIncome());
        }


        return Result.succ(list);
    }

}
